Page({
  data: {
    /** 显示删除确认框 */
    dialogVisible: false,
    /** 点击删除房屋的 id */
    houseId: '',
    /** 房屋列表 */
    houseList: [
      {
        building: "3",
        gender: 0,
        id: "6852666240028672",
        mobile: "13011112222",
        name: "张三",
        point: "华兴阁",
        room: "351",
        status: 1
      }
    ]
  },

  async onLoad() {
    this.getHouseList()
  },

  /** 获取房屋列表数据 */
  async getHouseList() {
    let res = await wx.http({
      url: '/room'
    })
    // console.log(res);
    this.setData({
      houseList: res.data
    })
  },

  /** 滑动后，点击 */
  swipeClose(ev) {
    const { position, instance } = ev.detail

    if (position === 'right') {
      console.log(ev.mark.id);
      // 显示 Dialog 对话框
      this.setData({
        dialogVisible: true,
        houseId: ev.mark.id
      })
    }
    // swiper-cell 滑块关闭
    instance.close()
  },

  /** 跳转详情页 */
  goDetail(ev) {
    // console.log(ev);
    let id = ev.mark.id
    wx.navigateTo({
      url: `/house_pkg/pages/detail/index?id=${id}`,
    })
  },

  addHouse() {
    wx.navigateTo({
      url: '/house_pkg/pages/locate/index',
    })
  },

  /** 删除房屋 */
  async dialogClose(ev) {
    if (ev.detail === 'confirm') {
      let res = await wx.http({
        url: '/room/' + this.data.houseId,
        method: 'DELETE'
      })
      if (res.code === 10000) {
        wx.showToast({ title: '删除成功', icon: 'success' })
        setTimeout(() => {
          this.getHouseList()
        }, 1000)
      }
    }
  }
})
